package com.zh.rent.comm;

import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Interceptor1 implements HandlerInterceptor {
    /*@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object ob = session.getAttribute("name");
        String urlString = request.getRequestURI();
        System.out.println("url:"+urlString);
        if (ob != null) {
            return true;
        }
       *//* if(urlString.equalsIgnoreCase("/view/index.html")){
            System.out.println("进入判断");
            return true;
        }*//*
        session.setAttribute("preurl", request.getRequestURI());
        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getServletContext().getContextPath()).append("/").toString();
        response.sendRedirect("/view/index.html");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response,
                           Object handler, ModelAndView model) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
                                Object handler, Exception ex) throws Exception {
    }*/
    protected org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler)throws Exception {

        HttpSession session = request.getSession();
        Object ob =null;
        ob = session.getAttribute("name");
        System.out.println("登录后："+ob);
        System.out.println("url:"+request.getRequestURI());
        if(null!=ob) {//已登录
            return true;
        }else {//未登录
            //直接重定向到登录页面
            //response.sendRedirect(request.getContextPath()+"/view/login.html");
            response.sendRedirect("/view/login.html");
            return false;
        }
    }

}
